$(function(){

    $.util.get($.util.getRequestJson('/dict/findByGroupKey.json'),{groupKey:"commtityType"},function(data){
        $('#commtityType').combobox({
            method:'post',
            panelHeight:200,//设置为固定高度，combobox出现竖直滚动条
            valueField:'dictKey',
            textField:'dictText',
            data:data.rows,
            multiple:true
            /*formatter: function (row) { //formatter方法就是实现了在每个下拉选项前面增加checkbox框的方法
                var opts = $(this).combobox('options');
                return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
            },
            onLoadSuccess: function () {  //下拉框数据加载成功调用
                var opts = $(this).combobox('options');
                var target = this;
                var values = $(target).combobox('getValues');//获取选中的值的values
                alert(values)
                $.map(values, function (value) {
                    var el = opts.finder.getEl(target, value);
                    el.find('input.combobox-checkbox')._propAttr('checked', true);
                })
            },
            onSelect: function (row) { //选中一个选项时调用
                var opts = $(this).combobox('options');
                //获取选中的值的values
                $("#commtityType").val($(this).combobox('getValues'));

                //设置选中值所对应的复选框为选中状态
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            },
            onUnselect: function (row) {//不选中一个选项时调用
                var opts = $(this).combobox('options');
                //获取选中的值的values
                $("#commtityType").val($(this).combobox('getValues'));

                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', false);
            }*/
        });
    });


    $("#detail").ueditor({
        height:250,
        template: false,
        toolbars: [[
            'fullscreen', 'source', '|', 'undo', 'redo', '|',
            'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
            'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
            'directionalityltr', 'directionalityrtl', 'indent', '|',
            'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
            'link', 'unlink', '|', 'simpleupload', '|', 'horizontal', 'date', 'time'
        ]]
    });

    var accept = {
        title: 'Images',
        extensions: 'gif,jpg,jpeg,bmp,png',
        mimeTypes: 'image/*'};
    $('#commName').text('商品图片');
    $("#attachedFile").webuploader({
        // 文件接收服务端。
        server: $.util.getRequestJson('/json/uploader.upload.json'),
        accept: accept,
        auto:true,
        fileNumLimit: 3 //限制多文件上传的个数
    });



    $('input[type=radio][name=isDiscount]').change(function() {
        if (this.value == '1') {
            $('#disPrice').removeAttr("style");
            //$('#disPrice').attr("data-options","required:false,prompt:'请输入优惠价格'");
            $('#disScore').removeAttr("style");
            //$('#disScore').attr("data-options","required:true");
        }else if(this.value == '0'){
            $('#disPrice').attr("style","display: none");
            $('#disScore').attr("style","display: none");
        }
    });


    var id = $.util.getRequest('id');
    if (id) {
        var url = $.util.getRequestJson('/json/productManage.productDetail.json');
        $.util.get(url, {id: id}, function (data) {
            setTimeout(function () {
                var dictKeyBack = data.data.dictKey;
                var dictKey = dictKeyBack.split(",");
                var isDiscount = data.data.isDiscount;
                $("#commtityType").combobox('setValues',dictKey);
                if(isDiscount == 1){
                    $('#disPrice').removeAttr("style");
                    $('#disScore').removeAttr("style");
                }
                $('#productAdd').form('load', data.data);
            }, 100);
        });
    }
})